package com.itcast.springboot.service.impl;

import com.itcast.springboot.bean.Admin;
import com.itcast.springboot.bean.CommonResult;
import com.itcast.springboot.bean.Feedback;
import com.itcast.springboot.mapper.AdminMapper;
import com.itcast.springboot.mapper.FeedbackMapper;
import com.itcast.springboot.service.AdminService;
import com.itcast.springboot.service.FeedbackService;
import com.itcast.springboot.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public Integer insertFeedbackUpload(Feedback feedback, MultipartFile feedbackSrc, MultipartFile feedbackSrc2) {
        String localPath = null;
        String localPath2 = null;
        //2:代表上传的是用户反馈的图片
        if (feedbackSrc != null) {
            //上传文件
            localPath = FileUploadUtils.fileUpload(2, feedbackSrc);
            if (localPath != null) {
                feedback.setSrc(localPath);
            }
        }
        if (feedbackSrc2 != null) {
            localPath2 = FileUploadUtils.fileUpload(2, feedbackSrc2);
            if (localPath2 != null) {
                feedback.setSrc2(localPath2);
            }
        }
        feedbackMapper.insertFeedback(feedback);
        return feedback.getFbid();
    }

    @Override
    public Integer insertFeedback(Feedback feedback) {
        return feedbackMapper.insertFeedback(feedback);
    }

    @Override
    public String insertFeedbackSrc(MultipartFile feedbackSrc) {
        if (feedbackSrc == null){
            return "图片不存在";
        }
        String serverPath = FileUploadUtils.fileUpload(2, feedbackSrc);
        if (serverPath == null){
            return "图片保存失败";
        }
        return serverPath;
    }

    @Override
    public String insertFeedbackSrc2(MultipartFile feedbackSrc2) {
        if (feedbackSrc2 == null){
            return "图片不存在";
        }
        String serverPath = FileUploadUtils.fileUpload(2, feedbackSrc2);
        if (serverPath == null){
            return "图片保存失败";
        }
        return serverPath;
    }


    @Override
    public Integer deleteFeedbackTrim(Integer isRead, Integer isSolve) {

        return feedbackMapper.deleteFeedbackTrim(isRead, isSolve);
    }

    @Override
    public Integer deleteFeedbackById(Integer fbid) {
        return feedbackMapper.deleteFeedbackById(fbid);
    }


    @Override
    public List<Feedback> getFeedbackSolvesTrim(Integer isRead, Integer isSolve) {
        return feedbackMapper.getFeedbackSolvesTrim(isRead, isSolve);
    }

    @Override
    public Integer updateFeedback(Feedback feedback) {
        if (feedback.getIsRead() == 0 && feedback.getIsSolve() == 1) {
            return 0;
        }
        return feedbackMapper.updateFeedback(feedback);
    }
}
